/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet.bpm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.FillableFeature;
import self.amigo.elem.bpm.StageView;
import self.amigo.elem.uml.NameCompartmentFeature;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IEditableElement;
import self.gee.editor.IPropertySheet;

public class StageSheet extends APropertySheet implements IPropertySheet {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane stageViews = new JTabbedPane();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel generalPanel = new JPanel();
  JLabel nameLabel = new JLabel();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  JTextField name = new JTextField();
  JLabel stereotypeLabel = new JLabel();
  JTextField stereotype = new JTextField();
  JLabel namespaceLabel = new JLabel();
  JTextField namespace = new JTextField();
  JLabel aidLinkLabel = new JLabel();
  JTextField aidLink = new JTextField();
  BorderLayout borderLayout2 = new BorderLayout();

  BorderLayout generalRegionHostLayout = new BorderLayout();
  JScrollPane generalRegionScroller = new JScrollPane();
  JPanel      generalTab = new JPanel();
  JLabel colorLabel = new JLabel();
  JPanel colorsPanel = new JPanel();
  VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
  JPanel jPanel1 = new JPanel();
  JLabel shevronLabel = new JLabel();
  JButton shevronColorBtn = new JButton();
  JLabel shevronColorLabel = new JLabel();
  JButton bkgGrad1ColorBtn = new JButton();
  JLabel bkgColor1Label = new JLabel();
  JLabel bkg1Label = new JLabel();
  JButton bkgGrad2ColorBtn = new JButton();
  JLabel bkgColor2Label = new JLabel();
  JLabel bkg2Label = new JLabel();
  JLabel compartmentColorLabel = new JLabel();
  JButton compartmentColorBtn = new JButton();
  JLabel compartmentLabel = new JLabel();
  GridBagLayout gridBagLayout1 = new GridBagLayout();

  private IEditableElement view;
  private NameCompartmentFeature     general;
  private FillableFeature shevronColorFf, bkgColor1Ff, bkgColor2Ff, compartmentColorFf;
  private Color shevronColor, bkgColor1, bkgColor2, compartmentColor;

  public StageSheet() {
    try {
      jbInit();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(name);
    nameLabel.setText("name");
    name.setFocusAccelerator('N');
    generalPanel.setLayout(verticalFlowLayout1);
    stereotypeLabel.setText("stereotype");
    namespaceLabel.setText("namespace");
    aidLinkLabel.setDisplayedMnemonic('0');
    aidLinkLabel.setText("aid");
    colorLabel.setText("color");
    colorsPanel.setLayout(verticalFlowLayout2);
    verticalFlowLayout2.setHorizontalFill(false);
    jPanel1.setLayout(gridBagLayout1);
    shevronLabel.setText("shevron");
    shevronColorBtn.setPreferredSize(new Dimension(30, 16));
    shevronColorBtn.setText("...");
    shevronColorBtn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        shevronColorBtn_actionPerformed(e);
      }
    });
    shevronColorLabel.setBackground(new Color(192, 255, 192));
    shevronColorLabel.setOpaque(true);
    shevronColorLabel.setText("               ");
    bkgGrad1ColorBtn.setPreferredSize(new Dimension(30, 16));
    bkgGrad1ColorBtn.setText("...");
    bkgGrad1ColorBtn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bkgGrad1ColorBtn_actionPerformed(e);
      }
    });
    bkgColor1Label.setBackground(new Color(192, 192, 12));
    bkgColor1Label.setOpaque(true);
    bkgColor1Label.setText("               ");
    bkg1Label.setText("background gradient 1");
    bkgGrad2ColorBtn.setPreferredSize(new Dimension(30, 16));
    bkgGrad2ColorBtn.setText("...");
    bkgGrad2ColorBtn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bkgGrad2ColorBtn_actionPerformed(e);
      }
    });
    bkgColor2Label.setBackground(new Color(192, 20, 192));
    bkgColor2Label.setOpaque(true);
    bkgColor2Label.setText("               ");
    bkg2Label.setForeground(Color.blue);
    bkg2Label.setText("background gradiant 2");
    compartmentColorLabel.setBackground(new Color(255, 192, 192));
    compartmentColorLabel.setOpaque(true);
    compartmentColorLabel.setText("               ");
    compartmentColorBtn.setPreferredSize(new Dimension(30, 16));
    compartmentColorBtn.setText("...");
    compartmentColorBtn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        compartmentColorBtn_actionPerformed(e);
      }
    });
    compartmentLabel.setText("compartment");
    generalPanel.add(nameLabel, null);
    generalPanel.add(name, null);
    generalPanel.add(stereotypeLabel, null);
    generalPanel.add(stereotype, null);
    generalPanel.add(namespaceLabel, null);
    generalPanel.add(namespace, null);
    generalPanel.add(aidLinkLabel, null);
    generalPanel.add(aidLink, null);
    generalPanel.add(colorLabel, null);
    generalPanel.add(colorsPanel, null);
    colorsPanel.add(jPanel1, null);
    jPanel1.add(shevronColorLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 24, 10));
    jPanel1.add(shevronColorBtn, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -14, 0));
    jPanel1.add(shevronLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 31, 10));
    jPanel1.add(bkgColor1Label, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 24, 10));
    jPanel1.add(bkgGrad1ColorBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -14, 0));
    jPanel1.add(bkg1Label, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 10));
    jPanel1.add(bkgColor2Label, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 24, 10));
    jPanel1.add(bkgGrad2ColorBtn, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -14, 0));
    jPanel1.add(bkg2Label, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 10));
    jPanel1.add(compartmentColorLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 24, 10));
    jPanel1.add(compartmentColorBtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -12, 0));
    jPanel1.add(compartmentLabel, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 2, 10));

    generalTab.setLayout( generalRegionHostLayout );
    generalTab.add( generalRegionScroller, BorderLayout.CENTER );
    generalRegionScroller.getViewport().add( generalPanel, null );


    this.add(stageViews, BorderLayout.CENTER);
    stageViews.add(generalTab, "General");
  }

  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof StageView ) {
      view = (IEditableElement) toDisplay;
      revert();
    } else
      throw new RuntimeException( "Invalid Graphic element to display:" + toDisplay );
  }

  public void accept() {
    if ( view == null )
      return;
    general.name = name.getText();

    general.stereotype = stereotype.getText();
    general.nameSpace = namespace.getText();
    general.aidLink = aidLink.getText();

    shevronColorFf.fillColor = shevronColor;
    bkgColor1Ff.fillColor = bkgColor1;
    bkgColor2Ff.fillColor = bkgColor2;
    compartmentColorFf.fillColor = compartmentColor;

    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    cmd.executeIfRequired();
  }

  public void revert() {
    if ( view == null )
      return;
    store.clear();
    view.getProperties( store );

    general = (NameCompartmentFeature) store.get( StageView.NAMECOMPARTMENT_PROP );
    shevronColorFf = (FillableFeature) store.get( StageView.FILL_PROP );
    bkgColor1Ff = (FillableFeature) store.get( StageView.FILL_BACKGROUND1 );
    bkgColor2Ff = (FillableFeature) store.get( StageView.FILL_BACKGROUND2 );
    compartmentColorFf = (FillableFeature) store.get( StageView.FILL_COMPARTMENT );

    shevronColor = shevronColorFf.fillColor;
    bkgColor1 = bkgColor1Ff.fillColor;
    bkgColor2 = bkgColor2Ff.fillColor;
    compartmentColor = compartmentColorFf.fillColor;

    shevronColorLabel.setBackground( shevronColorFf.fillColor );
    bkgColor1Label.setBackground( bkgColor1Ff.fillColor );
    bkgColor2Label.setBackground( bkgColor2Ff.fillColor );
    compartmentColorLabel.setBackground( compartmentColorFf.fillColor );

    name.setText( general.name );
    stereotype.setText( general.stereotype );
    namespace.setText( general.nameSpace );
    aidLink.setText( general.aidLink );
  }

  public Object getObjectUnderInspection() {
    return view;
  }

  void shevronColorBtn_actionPerformed(ActionEvent e) {
    Color c = querySelectColor( "Select Shevron color", shevronColor );
    if ( c != null ) {
      shevronColor = c;
      shevronColorLabel.setBackground( c );
    }
  }

  void bkgGrad1ColorBtn_actionPerformed(ActionEvent e) {
    Color c = querySelectColor( "Select Background gradiend 1 color", bkgColor1 );
    if ( c != null ) {
      bkgColor1 = c;
      bkgColor1Label.setBackground( c );
    }
  }

  void bkgGrad2ColorBtn_actionPerformed(ActionEvent e) {
    Color c = querySelectColor( "Select Background gradiend 2 color", bkgColor2 );
    if ( c != null ) {
      bkgColor2 = c;
      bkgColor2Label.setBackground( c );
    }
  }

  void compartmentColorBtn_actionPerformed(ActionEvent e) {
    Color c = querySelectColor( "Select Compartment color", compartmentColor );
    if ( c != null ) {
      compartmentColor = c;
      compartmentColorLabel.setBackground( c );
    }
  }

  private Color querySelectColor( String title, Color curr ) {
		return JColorChooser.showDialog( this, title,  curr );
  }
//  static public void main( String[] args ) {
//    JFrame form = new JFrame( "test" );
//    StageSheet sheet = new StageSheet();
//    form.getContentPane().add( sheet );
//    form.show();
//  }
}
